import fs from 'fs-extra';
import path from 'path';

(async function () {
  const template = await fs.readFile(path.join(__dirname, 'dist/client/index.html'), 'utf8');
  let routes = await fs.readdir('./src/routes');
  let routeNames = routes.map((route: string) => {
    let name = path.basename(route, '.tsx').toLowerCase();
    return name;
  });
  const { render } = require('./dist/server/server-entry.js');
  const staticDir = path.join(__dirname, 'dist/static');
  await fs.ensureDir(staticDir);
  for (let routeName of routeNames) {//home user profile
    const appHtml = await render('/' + (routeName === 'home' ? '' : routeName));
    const routeHtml = template.replace('<!--ssr-outlet-->', appHtml);
    if (routeName === 'home') routeName = 'index';
    await fs.writeFile(path.join(__dirname, 'dist/static', `${routeName}.html`), routeHtml);
  }
  await fs.copy(path.join(__dirname, 'dist/client/assets'), path.join(__dirname, 'dist/static/assets'));
})()